package com.xzm.project.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;

import androidx.annotation.Nullable;

import cn.wuyu.baselibrary.utils.AppUtil;
import cn.wuyu.baselibrary.utils.DeviceUtil;
import cn.wuyu.baselibrary.utils.ToastUtil;

/**
 * author : xuzongmeng
 * date   : 2019/9/6
 * desc   : 数字锁
 */
public class NumberLockView extends View {
    private int mScreenWidth = DeviceUtil.getDeviceWidth(AppUtil.getAppContext());
    private int mScreenHeight = DeviceUtil.getDeviceWidth(AppUtil.getAppContext());

    private float[] xPosition = new float[3];
    private float[] yPosition = new float[4];

    private float circleX, circleY; //
    private float circleR = 80; //半径

    private int number;


    private Paint mCirclePaint;
    private Paint mTextPaint;

    public NumberLockView(Context context) {
        this(context, null);
    }

    public NumberLockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberLockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
        setBackgroundColor(Color.GREEN);
    }

    private void init(AttributeSet attrs) {
         float firstX = 0;
         float firstY = 0;
        firstX = (float) mScreenWidth / 4;
        firstY = (float) (mScreenHeight - mScreenHeight / 3) / 4;

        xPosition[0] = firstX;
        xPosition[1] = firstX * 2;
        xPosition[2] = firstX * 3;

        yPosition[0] = firstY;
        yPosition[1] = firstY + firstX;
        yPosition[2] = firstY + firstX * 2;
        yPosition[3] = firstX + firstX * 3;

        mTextPaint = new Paint();
        mTextPaint.setTextSize(50);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setStrokeWidth(2);
        mTextPaint.setTextAlign(Paint.Align.CENTER);


        mCirclePaint = new Paint();
        mCirclePaint.setTextSize(50);
        mCirclePaint.setColor(Color.RED);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(10);

    }


//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
//    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTextNumber(canvas);
        drawCircle(canvas);
        setClickable(false);
      if (circleX>0&&circleY>0){
          if(eventType==MotionEvent.ACTION_DOWN){
              mCirclePaint.setStyle(Paint.Style.FILL);
              canvas.drawCircle(circleX,circleY,circleR,mCirclePaint);
          } else if (eventType==MotionEvent.ACTION_UP){
              mCirclePaint.setStyle(Paint.Style.STROKE);
              canvas.drawCircle(circleX,circleY,circleR,mCirclePaint);
              circleX=0;
              circleY=0;
          }
      }
    }


    private float yOffset = 20;

    private void drawTextNumber(Canvas canvas) {

        canvas.drawText("1", xPosition[0], yPosition[0] + yOffset, mTextPaint);
        canvas.drawText("2", xPosition[1], yPosition[0] + yOffset, mTextPaint);
        canvas.drawText("3", xPosition[2], yPosition[0] + yOffset, mTextPaint);

        canvas.drawText("4", xPosition[0], yPosition[1] + yOffset, mTextPaint);
        canvas.drawText("5", xPosition[1], yPosition[1] + yOffset, mTextPaint);
        canvas.drawText("6", xPosition[2], yPosition[1] + yOffset, mTextPaint);

        canvas.drawText("7", xPosition[0], yPosition[2] + yOffset, mTextPaint);
        canvas.drawText("8", xPosition[1], yPosition[2] + yOffset, mTextPaint);
        canvas.drawText("9", xPosition[2], yPosition[2] + yOffset, mTextPaint);


    }

    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(xPosition[0], yPosition[0], circleR, mCirclePaint);
        canvas.drawCircle(xPosition[1], yPosition[0], circleR, mCirclePaint);
        canvas.drawCircle(xPosition[2], yPosition[0], circleR, mCirclePaint);

        canvas.drawCircle(xPosition[0], yPosition[1], circleR, mCirclePaint);
        canvas.drawCircle(xPosition[1], yPosition[1], circleR, mCirclePaint);
        canvas.drawCircle(xPosition[2], yPosition[1], circleR, mCirclePaint);

        canvas.drawCircle(xPosition[0], yPosition[2], circleR, mCirclePaint);
        canvas.drawCircle(xPosition[1], yPosition[2], circleR, mCirclePaint);
        canvas.drawCircle(xPosition[2], yPosition[2], circleR, mCirclePaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getY();
                eventType=MotionEvent.ACTION_DOWN;
                handleDown(x, y);
                break;
            case MotionEvent.ACTION_UP:
                eventType =MotionEvent.ACTION_UP;
                setDefault();
                invalidate();
                sendAccessEvent("抬起");
                break;
            case MotionEvent.ACTION_CANCEL:
                eventType =MotionEvent.ACTION_CANCEL;
                setDefault();
                break;


        }
        return false;
    }
    /*
     * 判断刷新数据
     * -1 不进行数据刷新
     * 0  按下刷新
     * 1  弹起刷新
     */
    private   int eventType=-1;
    private void setDefault(){
        circleX = 0;
        circleY = 0;
        eventType=-1;
        sendAccessEvent("取消");
    }

    private void sendAccessEvent(String text){
        setContentDescription(text);
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
        setContentDescription(null);
    }
    private void handleDown(float x, float y) {
        if (xPosition[0] - circleR < x && x < xPosition[0] + circleR) {
            //第一列
            circleX = xPosition[0];
            if (yPosition[0] - circleR < y && y < yPosition[0] + circleR) {
                //第一排
                number=1;
                circleY = yPosition[0];
            } else if (yPosition[1] - circleR < y && y < yPosition[1] + circleR) {
                //第二排
                number=4;
                circleY = yPosition[1];
            } else if (yPosition[2] - circleR < y && y < yPosition[2] + circleR) {
                //第三排
                number=7;
                circleY = yPosition[2];
            }

        } else if (xPosition[1] - circleR < x && x < xPosition[1] + circleR) {
            circleX = xPosition[1];
            //第二列
            if (yPosition[0] - circleR < y && y < yPosition[0] + circleR) {
                //第一排
                number=2;
                circleY = yPosition[0];
            } else if (yPosition[1] - circleR < y && y < yPosition[1] + circleR) {
                //第二排
                number=5;
                circleY = yPosition[1];
            } else if (yPosition[2] - circleR < y && y < yPosition[2] + circleR) {
                //第三排
                number=8;
                circleY = yPosition[2];
            }else if (yPosition[3] - circleR < y && y < yPosition[3] + circleR) {
                //第四排
                number=0;
                circleY = yPosition[3];
            }
        } else if (xPosition[2] - circleR < x && x < xPosition[2] + circleR) {
            //第三列
            circleX = xPosition[2];
            if (yPosition[0] - circleR < y && y < yPosition[0] + circleR) {
                //第一排
                number=3;
                circleY = yPosition[0];
            } else if (yPosition[1] - circleR < y && y < yPosition[1] + circleR) {
                //第二排
                number=6;
                circleY = yPosition[1];
            } else if (yPosition[2] - circleR < y && y < yPosition[2] + circleR) {
                //第三排
                number=9;
                circleY = yPosition[2];
            }
        }
        sendAccessEvent("按下");
        invalidate();
//        Toast.makeText(getContext(), "点击" + number, Toast.LENGTH_SHORT).show();

        ToastUtil.showTest("点击" + number);

    }
}
